<?php
namespace App\Api\Fcjd;

use PhalApi\Api;


/**
 * 获取下级代理接口
 * @desc 读取phone_kami表数据
 */
class UserAgent extends Api {
    public function getRules() {
        return array(
            'getList' => array(
                'page' => array('name' => 'page', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '页码'),
                'page_size' => array('name' => 'page_size', 'type' => 'int', 'min' => 1, 'max' => 500, 'require' => true, 'desc' => '每页数量'),
                'Invitation' => array('name' => 'Invitation', 'type' => 'string', 'require' => true, 'desc' => '用户名')
            ),
            'getPhoneList' => array(
                'page' => array('name' => 'page', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '页码'),
                'page_size' => array('name' => 'page_size', 'type' => 'int', 'min' => 1, 'max' => 500, 'require' => true, 'desc' => '每页数量'),
                'username' => array('name' => 'username', 'type' => 'string', 'require' => true, 'desc' => '用户名')
            ),
        );
    }

    /**
     * 获取kami列表
     * @desc 获取phone_kami表数据
     * @return array items 数据列表
     * @return int total 总记录数
     */
    public function getPhoneList() {
        $page = $this->page;
        $pageSize = $this->page_size;

        $where = array(
            'username' => $this->username,
        );
        
        $total = \PhalApi\DI()->notorm->phone_list->where($where)->count();
        $items = \PhalApi\DI()->notorm->phone_list
            ->select('id,username,mobile,password,time_line')
            ->where($where)
            // ->order('kami_code DESC')
            ->limit(($page - 1) * $pageSize, $pageSize);

        \PhalApi\DI()->response->addResult('success', true);
        return array('items' => $items, 'total' => $total, 'page' => $page, 'page_size' => $pageSize);
    }

    /**
     * 获取kami列表
     * @desc 获取phone_kami表数据
     * @return array items 数据列表
     * @return int total 总记录数
     */
    public function getList() {
        $page = $this->page;
        $pageSize = $this->page_size;

        $where = array(
            'Invitation' => $this->Invitation,
        );
        
        $total = \PhalApi\DI()->notorm->phalapi_user->where($where)->count();
        $items = \PhalApi\DI()->notorm->phalapi_user
            ->select('id,username,nickname,mobile,phone,Invitation')
            ->where($where)
            // ->order('kami_code DESC')
            ->limit(($page - 1) * $pageSize, $pageSize);

        $itemAgents = array();
        foreach ($items as $key => $value) {
            $itemAgents[$key] = array();
            $itemAgents[$key]['id'] = $value['id'];
            $itemAgents[$key]['username'] = $value['username'];
            $itemAgents[$key]['nickname'] = $value['nickname'];
            $itemAgents[$key]['mobile'] = $value['mobile'];
            $itemAgents[$key]['phone'] = $value['phone'];
            $itemAgents[$key]['Invitation'] = $value['Invitation'];
            $itemAgents[$key]['count'] = \PhalApi\DI()->notorm->phone_list->where('username', $value['username'])->count();
            $itemAgents[$key]['next'] = \PhalApi\DI()->notorm->phalapi_user->where('Invitation', $value['username'])->count();
        }

        // return 123;

        \PhalApi\DI()->response->addResult('success', true);
        return array('items' => $itemAgents, 'total' => $total, 'page' => $page, 'page_size' => $pageSize);
    }
    
    
}